package com.wing.warehouse.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.warehouse.model.entity.WarehouseInventoryType;
import com.wing.warehouse.model.request.WarehouseInventoryTypeForm;
import com.wing.warehouse.service.WarehouseInventoryTypeService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author
 */
@Validated
@Api(value = "Admin-出入库类型管理", tags = "#/warehouse/warehouseInventoryType")
@RestController("warehouseInventoryTypeController")
@RequestMapping("warehouse/admin/warehouseInventoryType")
@RequiredArgsConstructor
public class WarehouseInventoryTypeController {

    private final WarehouseInventoryTypeService warehouseInventoryTypeService;


    @ApiOperation(value = "查询所有出入库类型", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseInventoryType> list(
            @RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "10") Integer limit
            , @RequestParam(defaultValue = "create_time") String sort
            , @RequestParam(defaultValue = "asc") String order
            , String code
            , String name
            , WarehouseInventoryType.Status status
            , WarehouseInventoryType.InventoryType inventoryType
            , String ids
            ,String supportReturn
    ) {

        Page<WarehouseInventoryType> pg = new Page<>(page, limit);
        QueryWrapper<WarehouseInventoryType> queryWrapper = new QueryWrapper<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }
        if (StringUtils.isNotBlank(supportReturn)) {
            queryWrapper.eq("support_return", supportReturn);
        }
        //搜索
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.like("code", code);
        }

        if (StringUtils.isNotBlank(ids)) {
            String[] split = ids.split(",");
            List<String> strings = Arrays.asList(split);
            queryWrapper.in("id", strings);
        }

        if (status != null) {
            queryWrapper.eq("status", status.getValue());
        }
        if (inventoryType != null) {
            queryWrapper.eq("inventory_type", inventoryType.getValue());
        }

        IPage<WarehouseInventoryType> list = warehouseInventoryTypeService.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加出入库类型", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(WarehouseInventoryTypeForm.Add.class) WarehouseInventoryTypeForm warehouseInventoryTypeForm) {
        if (warehouseInventoryTypeService.add(warehouseInventoryTypeForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改出入库类型", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(WarehouseInventoryTypeForm.Update.class) WarehouseInventoryTypeForm warehouseInventoryTypeForm) {
        WarehouseInventoryType warehouseInventoryType = warehouseInventoryTypeService.getById(warehouseInventoryTypeForm.getId());
        if (warehouseInventoryType == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(WarehouseInventoryType.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(warehouseInventoryTypeForm, warehouseInventoryType, copyOptions);
        if (warehouseInventoryTypeService.updateById(warehouseInventoryType)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


    @ApiOperation(value = "删除出入库类型", notes = "删除")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long warehouseInventoryTypeId) {
        if (warehouseInventoryTypeService.removeById(warehouseInventoryTypeId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "检查唯一性")
    @GetMapping("/isOnly")
    public JsonResult isOnly(@RequestParam(name = "code", required = false) String code, @RequestParam(name = "name", required = false) String name, @RequestParam(name = "id", required = false) Long id) {
        QueryWrapper<WarehouseInventoryType> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(code)) {
            queryWrapper.eq("code", code);
        }
        if (StringUtils.isNotBlank(name)) {
            queryWrapper.eq("name", name);
        }
        if (id != null) {
            queryWrapper.ne("id", id);
        }
        int count = warehouseInventoryTypeService.count(queryWrapper);
        return JsonResult.success(count == 0);
    }

    @ApiOperation(value = "查询所有出入库类型", notes = "删除")
    @GetMapping("/getAll")
    public JsonResult getAll(Integer inventoryType) {
        QueryWrapper<WarehouseInventoryType> wrapper = new QueryWrapper<>();
        wrapper.eq("inventory_type", inventoryType);
        wrapper.eq("status", 1);
        wrapper.select("id", "code", "name");
        List<WarehouseInventoryType> list = warehouseInventoryTypeService.list(wrapper);
        return JsonResult.success(list);
    }

    @ApiOperation(value = "查询出入库类型详细", notes = "查询")
    @GetMapping("/get")
    public JsonResult get(@RequestParam(name = "inventoryTypeId", required = true) Long inventoryTypeId) {
        WarehouseInventoryType warehouseInventoryType = warehouseInventoryTypeService.getById(inventoryTypeId);
        return JsonResult.success(warehouseInventoryType);
    }

}
